// JavaScript Document

//plugin
(function($) {	
	$.fn.coverbg = function(options) {
		options = $.extend({
			center:		false,
			width:		800,
			height:		600
		}, options);
		
		return this.each(function() {
			var $this = $(this),
			$container = $this.parent(),
			center = options.center,
			img_aspect_ratio = options.height / options.width;
			
			function resize() {
				var container_aspect_ratio = $container.height() / $container.width();
				if(img_aspect_ratio >= container_aspect_ratio) {
					$this.css({"width":"100%", "height":"auto"});
					if(center) $this.css({"margin-top":0, "margin-left":0});
				} else {
					$this.css({"width":"auto", "height":"100%", "margin-top":0, "margin-left":0});
					if(center) $this.css({"margin-top":0, "margin-left":0});
				}
			}
			
			resize();
			$(window).on("resize", resize);
		});
	};
})(jQuery);

//print
$("a.print").click(function(){window.print(); });

//load images
if($.browser.version != '7.0' && $.browser.version != '8.0')
{
	$("img").css("opactity","0");
	$("img").imagesLoaded({
		progress: function (isBroken, $images, $proper, $broken) {
			$(this).fadeTo("medium", 1);
		},
		always: function() {
			$("img").css("opacity", 1);
		}
	});
}

$("#aside").children("div").prepend('<div class="label"></div>');

//background
$("#content").children("img.background").coverbg({width:846, height:589});
$("#photo").children("img.background").coverbg({width:1400, height:409});

//slider
$("ul.slidetabs").tabs("img.background.slide", {
	effect:"fade",
	fadeOutSpeed:1000,
	fadeInSpeed:1000,
	rotate:true,
	initialIndex:Math.floor(Math.random() * $("ul.slidetabs li").length)
}).slideshow({autoplay:true, autopause:false, interval:4000, clickable:false});
$("ul.accordeon").tabs("ul.accordeon ul", {effect:"slide", initialIndex:null});

$(window).on({
	resize: function() {
		var height = $("#main").height() - 40,
			margin = 0;
			$active = $("#nav").find("li.active");
		$("#main div.wrapper").css("height", height);
		switch($active.index()) {
		case 0: margin = -8; break;
		case 1: margin = -10; break;
		}
		$("div.label").css({"top": $active.offset().top + margin, "height": $active.height() + 15});
	},
	load: function(){
		$("#main div.wrapper").mCustomScrollbar({
			scrollButtons:{enable:true},
			advanced:{updateOnContentResize:true},
			theme:"dark"
		});
	}
}).triggerHandler("resize");
//tabs
var tabIndex=0;
$("ul.tabs").tabs("div.pane", {tabs:"li:not(.disabled)", initialIndex:tabIndex});
$("ol.tabs").tabs("div.pane", {tabs:"li:not(.disabled)", initialIndex:tabIndex});

$("#slider").on({
	mouseenter: function() {
		$(this).children("ul.slidetabs, a.backward, a.forward").stop(true).hide().fadeIn();
	},
	mouseleave: function() {	
		$(this).children("ul.slidetabs, a.backward, a.forward").stop(true).fadeOut();
	}
});